package com.pp.controller;

import com.github.pagehelper.PageInfo;
import com.pp.entity.Buy;
import com.pp.entity.Game;
import com.pp.entity.Money;
import com.pp.entity.Shop;
import com.pp.service.SuccessService;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lanou on 18/2/1.
 */
@Controller
@RequestMapping("/success")
public class SuccessController {

    @Resource
    private SuccessService successService;

    @RequestMapping("games")
    public String games(Game game, HttpSession session){
        String userName = (String) session.getAttribute("name");
        Map<String,Object> map = successService.game(game,userName);
        Game game1 =(Game) map.get("game");
        Money money = (Money) map.get("money");
        int gid = game1.getId();
        int uid =(int) map.get("uid");
        if(money==null){
            successService.setMoney(uid,gid);
        }
        Money money1 = successService.queryMoney(uid,gid);


        session.setAttribute("uid", uid);
        session.setAttribute("gid", gid);
        session.setAttribute("money", money1.getMoney());
//        session.setAttribute("game",game1);
        session.setAttribute("p1", game1.getP1());
        session.setAttribute("p2", game1.getP2());
        session.setAttribute("p3", game1.getP3());
        session.setAttribute("img1", game1.getImg1());
        session.setAttribute("img2", game1.getImg2());
        session.setAttribute("img3", game1.getImg3());

        return "game";
    }
    @RequestMapping("/chongzhi")
    public String chongzhi(String jine, HttpSession session) {
        int jine1 = Integer.parseInt(jine);
        int uid = (int)session.getAttribute("uid");
        int gid = (int) session.getAttribute("gid");

        Money money = successService.pay(jine1,uid,gid);
        session.setAttribute("money", money.getMoney());
        return "game";
    }
    @RequestMapping("/shopping")
    public String shopping(int id,HttpSession session){
        int gid = (int)session.getAttribute("gid");
        List<Shop> list = successService.shopping(id,gid);
        session.setAttribute("dlist",list);
        return "shopping";
    }
    @RequestMapping("buy")
    @ResponseBody
    public Map buy(int did, HttpSession session){
        Map<String,Object> map = new HashMap<>();
        int uid = (int)session.getAttribute("uid");
        int gid = (int) session.getAttribute("gid");
        List<Buy> list = successService.getShop(did,uid);
        if(CollectionUtils.isEmpty(list)){
            int allMoney = 0;
            Map<String,Object> map1 =successService.buy(did,uid,gid);
            int userMoney =(int) map1.get("userMoney");
            int buyMoney =(int) map1.get("buyMoney");
            if(userMoney>buyMoney){
                allMoney = userMoney - buyMoney;
                successService.setBuy(did,uid,gid,allMoney);
                session.setAttribute("money",allMoney);
                return null;
            }else {
                map.put("data","你的钱不够，快去充值");
                return map;
            }
        }else {
            map.put("data","该物品已购买，无需重复购买");
            return map;
        }

    }
    @RequestMapping("buycar")
    public ModelAndView buycar(HttpSession session,ModelAndView mav,Integer pageNum){
        if(pageNum == null) {
            pageNum = 1;
        }
        int uid = (int) session.getAttribute("uid");
        PageInfo pageInfo =  successService.buycar(pageNum,uid);
        mav.addObject("shops",pageInfo);
        mav.setViewName("buycar");
        return mav;
    }
}
